<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\model\Comments;
use think\facade\Db;

class Comment extends BaseController
{
    public function index()
    {
        $keyword = request()->param('keyword');
        $data = Db::table('comments')->alias('c')
            ->leftJoin('product p ', 'c.product_id=p.id')
            ->leftJoin('users u', 'c.users_id=u.id')
            ->when($keyword, function ($query) use ($keyword) {
                //合并字符串 concat()
                $query->whereRaw('(concat(u.username,p.name) like "%' . $keyword . '%")');
            })->field('u.username ,p.name, c.*')->paginate(10)
            ->map(function ($item) {
                $item['created_at'] = date('Y-m-d H:i', $item['created_at']);
                return $item;
            });
        return $this->success($data);
    }

    public function update($id)
    {
        return $this->success(Comments::where('id', $id)->update(request()->param()));
    }
}
